/**
 * 
 */
package com.jeecms.wicool.action.distribution;

import static com.jeecms.common.page.SimplePage.cpn;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeecms.cms.web.WebErrors;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.dao.tradearea.OfficeBuildingDao;
import com.jeecms.wicool.entity.distribution.DistributionUser;
import com.jeecms.wicool.manager.distribution.DistributionUserMng;
import com.jeecms.wicool.util.MD5Util;

/**
 * @author Administrator
 *
 */
@Controller
public class DistributionUserAct {

	private static final Logger log = LoggerFactory.getLogger(DistributionUserAct.class);
	
	@Autowired
	private DistributionUserMng distributionUserMng;
	
	@RequestMapping("/distributionuser/v_list.do")
	public String list(String name,String mobile, Integer pageNo, HttpServletRequest request, ModelMap model) {
		Pagination pagination = distributionUserMng.getPage(name,mobile,cpn(pageNo), CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
		model.addAttribute("name", name);
		model.addAttribute("mobile", mobile);
		return "distributionuser/list_distributionuser";
	}
	
	@RequestMapping("/distributionuser/v_add.do")
	public String add(ModelMap model,HttpServletRequest request) {
		DistributionUser trade = new DistributionUser();
		model.addAttribute("user", trade);
		return "distributionuser/add_distributionuser";
	}
	
	@RequestMapping("/distributionuser/o_save.do")
	public String save(DistributionUser bean, Integer pageNo,HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		
		if(StringUtils.isBlank(bean.getPassword())){
			bean.setPassword(MD5Util.createMD5String("123456"));
		}else{
			bean.setPassword(MD5Util.createMD5String(bean.getPassword()));
		}
		bean = distributionUserMng.save(bean);
		log.info("save DistributionUser id={}", bean.getId());
		return "redirect:v_list.do";
	}
	
	@RequestMapping("/distributionuser/v_edit.do")
	public String edit(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		vldExist(id, errors);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		DistributionUser trade = distributionUserMng.findById(id);
		model.addAttribute("user", trade);
		return "distributionuser/edit_distributionuser";
	}
	
	@RequestMapping("/distributionuser/o_update.do")
	public String update(DistributionUser bean,String password_new,
			HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		vldExist(bean.getId(), errors);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		if(StringUtils.isNotBlank(password_new)){
			bean.setPassword(MD5Util.createMD5String(password_new));
		}
		bean = distributionUserMng.update(bean);
		model.addAttribute("message", "global.success");
		log.info("update DistributionUser success. id={}", bean.getId());
		return "redirect:v_list.do";
	}
	
	@RequestMapping("/distributionuser/o_delete.do")
	public String delete(Integer[] ids, String name,String loginName, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		WebErrors errors =  WebErrors.create(request);
		errors.ifEmpty(ids, "ids");
		for (Integer id : ids) {
			vldExist(id, errors);
		}
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		for (Integer id : ids) {
			if(!CollectionUtils.isEmpty(officeBuildingDao.getOfficeBuildingList(id))){
				model.addAttribute("message", "所选配送员已关联写字楼信息，无法删除！！！");
				return list(name, null, pageNo, request, model);
			}
		}
		List<DistributionUser> beans = distributionUserMng.deleteByIds(ids);
		for (DistributionUser bean : beans) {
			log.info("delete DistributionUser id={}", bean.getId());
		}
		return "redirect:v_list.do";
	}
	
	private boolean vldExist(Integer id, WebErrors errors) {
		if (errors.ifNull(id, "id")) {
			return true;
		}
		DistributionUser entity = distributionUserMng.findById(id);
		if (errors.ifNotExist(entity, DistributionUser.class, id)) {
			return true;
		}
		return false;
	}
	
	@Autowired
	private OfficeBuildingDao officeBuildingDao;
}
